
package taller2.controller.modes.supervisor;

import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import taller2.controller.modes.Mode;
import taller2.interfaces.Connection;
import taller2.interfaces.Graph;
import taller2.interfaces.Node;
import taller2.utils.Constants;
import taller2.utils.Point;
import taller2.view.draws.DrawingArea;
import taller2.view.draws.GraphDrawer;

public class PrimmSupervisorMode implements Mode {
	private Graph graph;
	private Node start,end;
	private boolean finished;
	
	private static final String invalidSelMsg = "please start by a selected node";
	
	// constructor
	public PrimmSupervisorMode(Graph graph){
		this.graph = graph;
		completeConnections();

		
		this.start = null;
		this.end = null;
		
		this.finished = false;
	}
	
	private void completeConnections() {
		for(Connection connection : graph.getAllConnections().values()){
			if(connection.isSelected()){
				connection.getThisNode().select();
				connection.getOtherNode().select();
			}
		}
	}

	// when pressed selects a node, if a node is selected already
	// it selects the connection from the first to the second
	public void whenPressed(MouseEvent arg0, DrawingArea area) {
		GraphDrawer drawer = area.getDrawer();
		Point pos = new Point(arg0.getX(),arg0.getY());
		if(!finished)
			supervise(drawer,area,pos);
	}
	
	private void supervise(GraphDrawer drawer, DrawingArea area,Point pos){
		if(start == null){
			start = drawer.getCloseByNode(pos);
			if((start != null)&&(!start.isSelected())){
				JFrame frame = new JFrame();
				JOptionPane.showMessageDialog(frame, invalidSelMsg);
				start = null;
			}
		}else{
			end = drawer.getCloseByNode(pos);
			if(end != null){
				if(end.isSelected()){
					end = null;
					JFrame frame = new JFrame();
					JOptionPane.showMessageDialog(frame,Constants.errorMsg);

				}else{
					if(responseWasCorrect(start,end)){
						start.selectConnectionsWith(end);
						start.select();
						end.select();
					}
				}
			}
			
			start = null;
			end = null;
		}
		
		if(allNodesSelected()){
			JFrame frame = new JFrame();
			JOptionPane.showMessageDialog(frame,Constants.okMsg);

			finished = true;
		}
			
		
		area.repaint();
	}

	private boolean allNodesSelected() {
		for(Node node : graph.getNodes()){
			if(!node.isSelected())
				return false;
		}
		return true;
	}

	private boolean responseWasCorrect(Node node1,Node node2){
		Connection connection = node1.getConnectionTo(node2);
		
		if(connection == null){
			JFrame frame = new JFrame();
			JOptionPane.showMessageDialog(frame, Constants.errorMsg);		
			return false;
		}
				
		for (Node node : graph.getNodes()){
			if(node.isSelected()){
				for(Connection conn : node.getConnections()){
					Node other = conn.getOtherNode();
					boolean isValid = !conn.isSelected() && !other.isSelected();
					boolean isSmaller = conn.getWeight() < connection.getWeight();
					
					if(isValid && isSmaller){
						JFrame frame = new JFrame();
						JOptionPane.showMessageDialog(frame,Constants.errorMsg);
						return false;
					}
				}
			}
		}	
		return true;
	}
	
	public void whenReleased(MouseEvent arg0, DrawingArea area) {}

	public void whenDragged(MouseEvent arg0, DrawingArea area) {}

}
